<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>智能会议室预约系统</h2>
        <p>用户登录</p>
      </div>
      
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <el-link type="primary" @click="toRegister">注册账号</el-link>
        <el-link type="primary" @click="forgotPassword">忘记密码</el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { userLoginService } from '../api/user'
import { ElMessage } from 'element-plus'
import { userTokenStore } from '../store/userStore'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const userStore = userTokenStore();

const loginForm = ref({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async() => {
  // loginFormRef.value.validate((valid) => {
  //   if (valid) {
  //     loading.value = true
  //     // 这里添加实际的登录逻辑
  //     // 模拟登录成功
  //     setTimeout(() => {
  //       loading.value = false
  //       ElMessage.success('登录成功')
  //       router.push('/main/home')
  //     }, 1000)
  //   }
  // })
  console.log(loginForm.value);
  let result = await userLoginService(loginForm.value)
    
    if(result.code === 0){
        //alert(result.message?result.message:'登录成功!')
        ElMessage.success('登录成功!')
        // console.log(result.data);
        userStore.setToken(result.data)
        router.push('/main/home')
    }else{
        //alert(result.message?result.message:'登录失败!')
        ElMessage.error(result.message?result.message:'登录失败!')
    }

}

const toRegister = () => {
  router.push('/register')
}

const forgotPassword = () => {
  router.push('/forgot-password')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-card {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin: 0 0 10px 0;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-button {
  width: 100%;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  width: 100%;
}
</style>
